<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><meta name="baidu-site-verification" content="1EB8XoOl0C"><meta name="google-site-verification" content="K7thEgdLm0UfRWJ5MGdF7sCcjClSzAlxFLPv2Oz5CGM"><title> Java基础 · 页面仔小杨</title><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="description" content="Java基础 - 页面仔小杨"><meta name="keywords" content="Java"><meta name="author" content="页面仔小杨"><link rel="short icon" href="/images/favicon.ico"><link rel="stylesheet" href="/css/bubuzou.css"><link rel="search" type="application/opensearchdescription+xml" href="http://itxing666.gitee.io/atom.xml" title="页面仔小杨"><script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='//unpkg.com/valine/dist/Valine.min.js'></script><meta name="generator" content="Hexo 4.2.0"><link rel="alternate" href="/atom.xml" title="页面仔小杨" type="application/atom+xml">
</head><body><header><div class="header row"> <a href="/" class="logo-link"><img src="/images/logo.png"></a><ul id="nav_list" class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" data-hover="博文" class="nav-list-link">博文</a></li><li class="nav-list-item"><a href="/archives/" target="_self" data-hover="归档" class="nav-list-link">归档</a></li><li class="nav-list-item"><a href="/about/" target="_self" data-hover="关于" class="nav-list-link">关于</a></li></ul><div class="search"><a id="search_btn" href="#search"></a></div><div id="nav_btn" class="nav-btn"><span></span><span></span><span></span></div></div></header><div class="row scroll-con"><section class="container"><!-- for archive page--><div id="postAr" class="post"><article class="post-block"><h1 class="post-title">Java基础</h1><div class="post-info">2021-01-18</div><div class="post-content"><p><img src="https://raw.githubusercontent.com/itxing666/blogImgs/main/img/204.png" alt="204"></p>
<h2 id="JDK-JRE-JVM"><a href="#JDK-JRE-JVM" class="headerlink" title="JDK JRE JVM"></a>JDK JRE JVM</h2><p>JDK: Java Development Kit</p>
<p>JRE: Java Runtime Environment</p>
<p>JVM: Java Virtual Machine</p>
<p>JDK = JRE + Java的开发工具（Javac.exe,java.exe,javadoc.exe）</p>
<p>JRD = JVM + Java核心类库</p>
<h2 id="搭建环境"><a href="#搭建环境" class="headerlink" title="搭建环境"></a>搭建环境</h2><p><a href="[http://www.oracle.com/technetwork/java/javase/downloads/](https://link.jianshu.com/?t=http%3A%2F%2Fwww.oracle.com%2Ftechnetwork%2Fjava%2Fjavase%2Fdownloads%2F)">官网下载JDK版本</a> 安装步骤省略</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">java -version</span><br></pre></td></tr></table></figure>



<h2 id="Hello-World"><a href="#Hello-World" class="headerlink" title="Hello World"></a>Hello World</h2><p>Hello.java</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Hello</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    System.out.print(<span class="string">"hello world"</span>);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>终端运行</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">javac Hello.java</span><br><span class="line">java Hello</span><br></pre></td></tr></table></figure>



<h2 id="Java基本语法"><a href="#Java基本语法" class="headerlink" title="Java基本语法"></a>Java基本语法</h2><h3 id="注释"><a href="#注释" class="headerlink" title="注释"></a>注释</h3><ul>
<li><p>单行  // Line comment</p>
</li>
<li><p>多行  /* Block comment */</p>
</li>
<li><p>文档</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">* <span class="doctag">@Description</span> HelloWorld</span></span><br><span class="line"><span class="comment">* <span class="doctag">@Author</span> itxing</span></span><br><span class="line"><span class="comment">*/</span></span><br></pre></td></tr></table></figure>

</li>
</ul>
<a id="more"></a>

<h3 id="标识符-关键字"><a href="#标识符-关键字" class="headerlink" title="标识符 关键字"></a>标识符 关键字</h3><p>关键字</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span>    <span class="keyword">extends</span> <span class="keyword">implements</span> <span class="title">null</span>      <span class="title">strictfp</span>     <span class="title">true</span></span></span><br><span class="line"><span class="class"><span class="title">assert</span>   <span class="title">const</span>    <span class="title">false</span>   <span class="title">import</span>     <span class="title">package</span>   <span class="title">super</span>        <span class="title">try</span></span></span><br><span class="line"><span class="class"><span class="title">boolean</span>  <span class="title">continue</span> <span class="title">final</span>   <span class="title">instanceof</span> <span class="title">private</span>   <span class="title">switch</span>       <span class="title">void</span></span></span><br><span class="line"><span class="class"><span class="title">break</span>    <span class="title">default</span>  <span class="title">finally</span> <span class="title">int</span>        <span class="title">protected</span> <span class="title">synchronized</span> <span class="title">volatile</span></span></span><br><span class="line"><span class="class"><span class="title">byte</span>     <span class="title">do</span>       <span class="title">float</span>   <span class="title">interface</span>  <span class="title">public</span>    <span class="title">this</span>         <span class="title">while</span></span></span><br><span class="line"><span class="class"><span class="title">case</span>     <span class="title">double</span>   <span class="title">for</span>     <span class="title">long</span>       <span class="title">return</span>    <span class="title">throw</span></span></span><br><span class="line"><span class="class"><span class="title">catch</span>    <span class="title">else</span>     <span class="title">goto</span>    <span class="title">native</span>     <span class="title">short</span>     <span class="title">throws</span></span></span><br><span class="line"><span class="class"><span class="title">char</span>     <span class="title">enum</span>     <span class="title">if</span>      <span class="title">new</span>        <span class="title">static</span>    <span class="title">transient</span></span></span><br></pre></td></tr></table></figure>

<p>标识符</p>
<p>用于给程序中的变量、类、方法命名的符号</p>
<ol>
<li>只能由字母（a-z，A-Z），数字（0-9），下划线（_）和美元符号（$）组成</li>
<li>不能以数字开头</li>
<li>不能与关键字重名</li>
<li>严格区分大小写</li>
</ol>
<p>  包名： 多单词组成时所有字母都小写：xxxyyyzzz</p>
<p>  类名、接口名：大驼峰 XxxYyyZxx</p>
<p>  变量名、方法名：小驼峰 xxxYyyZzz</p>
<p>  常量： 所有字母大写，XXX_YYY_ZZZ</p>
<h3 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h3><p>基本类型和引用类型</p>
<p>byte short int long char float double boolean</p>
<p>引用类型 </p>
<p>类 接口 数组  </p>
<p><img src="https://raw.githubusercontent.com/itxing666/blogImgs/main/img/144.png" alt="144"></p>
<p>二进制0b 八进制0  十六进制0x 开头</p>
<h3 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h3><ol>
<li><p>自动类型提升：byte short int long float double</p>
<p>char byte short 互相加减都要转换为int</p>
</li>
<li><p>强制转换 (类型)变量名</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">byte</span> b = (<span class="keyword">byte</span>)a</span><br></pre></td></tr></table></figure>
</li>
<li><p>不能对boolean进行转换</p>
</li>
<li><p>不能把对象类型转换为不相干的类型</p>
</li>
<li><p>高容量转低容量要强制转换，可能存在内存溢出、精度丢失问题</p>
</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> money = <span class="number">10_0000_0000</span>;</span><br><span class="line">System.out.println(money);</span><br></pre></td></tr></table></figure>



<h3 id="变量-常量"><a href="#变量-常量" class="headerlink" title="变量 常量"></a>变量 常量</h3><ul>
<li>每个变量都要有类型</li>
<li>变量名必须是合法标识符</li>
<li>变量声明是一个完整的语句，必须以分号结束</li>
</ul>
<p>类变量  实例变量  局部变量</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Variable</span> </span>&#123;</span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">int</span> allClicks = <span class="number">0</span>;  <span class="comment">// 类变量</span></span><br><span class="line">    String str = <span class="string">"hello world"</span>; <span class="comment">// 实例变量</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">0</span>; <span class="comment">// 局部变量 必须初始化值</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>常量（初始化后不能改变值，大写）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">final</span> <span class="keyword">double</span> PI = <span class="number">3.14</span>;</span><br></pre></td></tr></table></figure>



<h3 id="运算符"><a href="#运算符" class="headerlink" title="运算符"></a>运算符</h3><ul>
<li>算术运算符：+, -, *, /,  %,  ++, –</li>
<li>赋值运算符：=</li>
<li>关系远算符：&gt;, &lt;, &gt;=, &lt;=, ==, !=, instanceof</li>
<li>逻辑运算符：&amp;&amp;, ||, !</li>
<li>位运算符：&amp;, |, ^, ~, &gt;&gt;, &lt;&lt;</li>
<li>条件运算符：？ ：</li>
<li>扩展运算符：+= -= *= /=</li>
</ul>
<h3 id="包机制"><a href="#包机制" class="headerlink" title="包机制"></a>包机制</h3><p>包的本质就是文件夹，一般利用公司域名倒置作为包名</p>
<p>package/import/*</p>
<h3 id="JavaDoc"><a href="#JavaDoc" class="headerlink" title="JavaDoc"></a>JavaDoc</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; 参数说明</span><br><span class="line">@author 作者名</span><br><span class="line">@version 版本号</span><br><span class="line">@since 指明需要最早使用的jdk版本</span><br><span class="line">@params 参数名</span><br><span class="line">@return 返回值</span><br><span class="line">@throws 异常抛出</span><br><span class="line"></span><br><span class="line">javac -encoding utf-8 -charset utf-8 Doc.java</span><br></pre></td></tr></table></figure>



<h2 id="Java流程控制"><a href="#Java流程控制" class="headerlink" title="Java流程控制"></a>Java流程控制</h2><h3 id="用户交互Scanner"><a href="#用户交互Scanner" class="headerlink" title="用户交互Scanner"></a>用户交互Scanner</h3><p>获取用户输入（next nextLine）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 创建对象</span></span><br><span class="line">Scanner scan = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">System.out.println(<span class="string">"使用next方式接收："</span>);</span><br><span class="line"><span class="comment">// 判断用户是否输入</span></span><br><span class="line"><span class="keyword">if</span> (scan.hasNext()) &#123;</span><br><span class="line">String str = scan.next();</span><br><span class="line">System.out.println(<span class="string">"用户输入："</span> + str);</span><br><span class="line">&#125;</span><br><span class="line">scan.close();</span><br></pre></td></tr></table></figure>



<h3 id="顺序结构"><a href="#顺序结构" class="headerlink" title="顺序结构"></a>顺序结构</h3><p>从上到下顺序执行</p>
<p><img src="https://raw.githubusercontent.com/itxing666/blogImgs/main/img/194.png" alt=""></p>
<h3 id="选择结构"><a href="#选择结构" class="headerlink" title="选择结构"></a>选择结构</h3><h3 id="if"><a href="#if" class="headerlink" title="if"></a>if</h3><p><img src="https://raw.githubusercontent.com/itxing666/blogImgs/main/img/195.png" alt="195"></p>
<h3 id="switch"><a href="#switch" class="headerlink" title="switch"></a>switch</h3><p>// byte shart int char string</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">switch</span>(expression) &#123;</span><br><span class="line">  <span class="keyword">case</span> value:</span><br><span class="line">    <span class="comment">// 语句</span></span><br><span class="line">    <span class="keyword">break</span>;</span><br><span class="line">  <span class="keyword">default</span>:</span><br><span class="line">    <span class="comment">// 语句</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="循环结构"><a href="#循环结构" class="headerlink" title="循环结构"></a>循环结构</h2><h3 id="while"><a href="#while" class="headerlink" title="while"></a>while</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span> (i &lt; <span class="number">100</span>) &#123;</span><br><span class="line">  i++;</span><br><span class="line">  System.out.println(i);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="do-while"><a href="#do-while" class="headerlink" title="do while"></a>do while</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> i = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">do</span> &#123;</span><br><span class="line">  sum += i;</span><br><span class="line">  i++;</span><br><span class="line">&#125; <span class="keyword">while</span> (i &lt;= <span class="number">100</span>);</span><br><span class="line">System.out.println(sum);</span><br></pre></td></tr></table></figure>

<h3 id="for"><a href="#for" class="headerlink" title="for"></a>for</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt;= <span class="number">100</span>; i++) &#123;</span><br><span class="line">  sum += i;</span><br><span class="line">&#125;</span><br><span class="line">System.out.println(sum);</span><br></pre></td></tr></table></figure>

<h3 id="九九乘法表"><a href="#九九乘法表" class="headerlink" title="九九乘法表"></a>九九乘法表</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">1</span>; i &lt;= <span class="number">9</span>; i++) &#123;</span><br><span class="line">  <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt;= i; j++) &#123;</span><br><span class="line">    System.out.print(i+<span class="string">"*"</span>+j+<span class="string">"="</span>+(i*j) + <span class="string">"\t"</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  System.out.println();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="for-in"><a href="#for-in" class="headerlink" title="for in"></a>for in</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">int</span>[] numbers = &#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>&#125;;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">int</span> x:numbers) &#123;</span><br><span class="line">  System.out.println(x);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="break-amp-continue"><a href="#break-amp-continue" class="headerlink" title="break &amp; continue"></a>break &amp; continue</h3><p>break: 强制退出循环</p>
<p>continue: 终止某次循环</p>
<h2 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h2><h3 id="什么是方法"><a href="#什么是方法" class="headerlink" title="什么是方法"></a>什么是方法</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">修饰符 返回值类型 方法名(参数类型 参数名) &#123;</span><br><span class="line">  ...</span><br><span class="line">  方法体</span><br><span class="line">  ...</span><br><span class="line">  <span class="keyword">return</span> 返回值;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="方法的重载"><a href="#方法的重载" class="headerlink" title="方法的重载"></a>方法的重载</h3><p>有相同的函数名称，但形参不同的函数</p>
<h3 id="可变参数"><a href="#可变参数" class="headerlink" title="可变参数"></a>可变参数</h3><p>一个方法只能指定一个可变参数，它必须是方法的最后一个参数</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    Demo02 demo02 = <span class="keyword">new</span> Demo02();</span><br><span class="line">    demo02.test(<span class="number">1</span>, <span class="number">2</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">test</span><span class="params">(<span class="keyword">int</span>... i)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; i.length; j++) &#123;</span><br><span class="line">        System.out.println(i[j]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h3 id="递归"><a href="#递归" class="headerlink" title="递归"></a>递归</h3><p>递归头:边界  递归体：返回阶段  n * (n-1)</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span> <span class="title">f</span><span class="params">(<span class="keyword">int</span> n)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (n == <span class="number">1</span>) &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> n * f(n-<span class="number">1</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="数组"><a href="#数组" class="headerlink" title="数组"></a>数组</h2><h3 id="声明"><a href="#声明" class="headerlink" title="声明"></a>声明</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">int[] arr &#x3D; &#123;1,2,3&#125;;</span><br><span class="line">int[] arr &#x3D; new int[5];</span><br><span class="line">int[] arr &#x3D; new int[]&#123;1,2,3,4&#125;;</span><br></pre></td></tr></table></figure>

<p>其长度是固定的</p>
<p>必须是相同的数据类型</p>
<h3 id="数组的使用"><a href="#数组的使用" class="headerlink" title="数组的使用"></a>数组的使用</h3><p>for…in 循环</p>
<p>数组当作方法的参数</p>
<p>数组可作为返回值</p>
<h3 id="多维数组"><a href="#多维数组" class="headerlink" title="多维数组"></a>多维数组</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">int[][] arr &#x3D; &#123;&#123;1,2&#125;,&#123;1,2&#125;,&#123;1,2&#125;&#125;;</span><br></pre></td></tr></table></figure>



<h3 id="冒泡排序"><a href="#冒泡排序" class="headerlink" title="冒泡排序"></a>冒泡排序</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">int</span>[] sort(<span class="keyword">int</span>[] array) &#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; array.length - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        Boolean flag = <span class="keyword">false</span>;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; array.length - <span class="number">1</span> - i; j++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (array[j+<span class="number">1</span>] &lt; array[j]) &#123;</span><br><span class="line">                <span class="keyword">int</span> temp = array[j];</span><br><span class="line">                array[j] = array[j+<span class="number">1</span>];</span><br><span class="line">                array[j+<span class="number">1</span>] = temp;</span><br><span class="line">                flag = <span class="keyword">true</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (flag == <span class="keyword">false</span>) &#123;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> array;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>





<h2 id="面向对象"><a href="#面向对象" class="headerlink" title="面向对象"></a>面向对象</h2><p>以类的方式组织代码，以对象的形式封装数据</p>
<p>静态方法：static</p>
<p>实例方法</p>
<p>封装 继承 多态</p>
<h3 id="构造器"><a href="#构造器" class="headerlink" title="构造器"></a>构造器</h3><p>一个类什么都不写，它也会存在构造器</p>
<ul>
<li><p>必须和类的名字相同</p>
</li>
<li><p>必须没有返回类型，也不用写void</p>
</li>
<li><p>一旦定义了有参构造尽量定义无参构造，不会自动添加无参构造</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">public class Student &#123;</span><br><span class="line">    public Student() &#123;&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">public class Student &#123;</span><br><span class="line">    public Student() &#123;&#125;</span><br><span class="line">    public Student(string name) &#123;</span><br><span class="line">        this.name &#x3D; name;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



</li>
</ul>
<h3 id="封装"><a href="#封装" class="headerlink" title="封装"></a>封装</h3><p>高内聚 低耦合</p>
<p>set/get方法</p>
<h3 id="继承"><a href="#继承" class="headerlink" title="继承"></a>继承</h3><p>单继承</p>
<table>
<thead>
<tr>
<th>访问修饰符</th>
<th>本类</th>
<th>同包</th>
<th>子类</th>
<th>其他</th>
</tr>
</thead>
<tbody><tr>
<td>private</td>
<td>√</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>默认</td>
<td>√</td>
<td>√</td>
<td></td>
<td></td>
</tr>
<tr>
<td>protected</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td></td>
</tr>
<tr>
<td>public</td>
<td>√</td>
<td>√</td>
<td>√</td>
<td>√</td>
</tr>
</tbody></table>
<p><strong>super</strong></p>
<p>子类默认super()</p>
<p>super() 调用了父类的构造器</p>
<p>super 和 this 不能同时在构造方法中</p>
<p><strong>重写</strong></p>
<p>子类的方法和父类的方法必须要一致</p>
<p>若父类的引用指向了子类</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">B b = <span class="keyword">new</span> A(); <span class="comment">// B父 A子</span></span><br><span class="line">b.test();   <span class="comment">// 静态方法执行B的，非静态时子类重写（能执行哪些方法看左边）</span></span><br></pre></td></tr></table></figure>



<h3 id="多态"><a href="#多态" class="headerlink" title="多态"></a>多态</h3><p>1.static 方法。属于类，不属于实例</p>
<p>2.final 常量</p>
<p>3.private 方法</p>
<p>以上不可重写</p>
<p><strong>多态存在的三个必要条件</strong></p>
<ul>
<li>继承</li>
<li>重写</li>
<li>父类引用指向子类对象：<strong>Parent p = new Child();</strong></li>
</ul>
<h2 id="instance-of"><a href="#instance-of" class="headerlink" title="instance of"></a>instance of</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// Object &gt; Person &gt; Student</span></span><br><span class="line">Object object = <span class="keyword">new</span> Student();</span><br><span class="line">System.out.println(object <span class="keyword">instanceof</span> Student);  <span class="comment">// true</span></span><br><span class="line">System.out.println(object <span class="keyword">instanceof</span> Person);   <span class="comment">// true</span></span><br><span class="line">System.out.println(object <span class="keyword">instanceof</span> Object);   <span class="comment">// true</span></span><br><span class="line">System.out.println(object <span class="keyword">instanceof</span> Teacher);  <span class="comment">// false</span></span><br><span class="line">System.out.println(object <span class="keyword">instanceof</span> String);   <span class="comment">// false</span></span><br></pre></td></tr></table></figure>

<p>高转低强制转换</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Person ibj = <span class="keyword">new</span> Student();</span><br><span class="line">Student student = (Student) obj; <span class="comment">// ((Student) obj).test();</span></span><br></pre></td></tr></table></figure>



<h2 id="static"><a href="#static" class="headerlink" title="static"></a>static</h2><p>静态方法</p>
<p>静态变量</p>
<p>静态代码块（一次）</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">static</span> &#123;&#125;</span><br></pre></td></tr></table></figure>

<p>静态导入包</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> <span class="keyword">static</span> java.lang.Math.random;</span><br></pre></td></tr></table></figure>



<h2 id="抽象类"><a href="#抽象类" class="headerlink" title="抽象类"></a>抽象类</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Employee</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">   <span class="keyword">private</span> String name;</span><br><span class="line">   <span class="keyword">private</span> String address;</span><br><span class="line">   <span class="keyword">private</span> <span class="keyword">int</span> number;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">double</span> <span class="title">computePay</span><span class="params">()</span></span>;</span><br><span class="line">   <span class="comment">//其余代码</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>不能new抽象类，只能子类继承去实现它</p>
<p>抽象方法必须在抽象类中</p>
<h2 id="接口"><a href="#接口" class="headerlink" title="接口"></a>接口</h2><ul>
<li><p>接口不能用于实例化对象。</p>
</li>
<li><p>接口没有构造方法。</p>
</li>
<li><p>接口中所有的方法必须是抽象方法。Java 8 之后 接口中可以使用 default 关键字修饰的非抽象方法。</p>
</li>
<li><p>接口不能包含成员变量，除了 static 和 final 变量。</p>
</li>
<li><p>接口不是被类继承了，而是要被类实现。</p>
</li>
<li><p>接口支持多继承。</p>
<p><strong>public abstract</strong>方法     <strong>public static final</strong> 变量</p>
</li>
</ul>
<p>接口多继承</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">Animal</span> </span>&#123;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>;</span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">travel</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MammalInt</span> <span class="keyword">implements</span> <span class="title">Animal</span></span>&#123;</span><br><span class="line"> </span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eat</span><span class="params">()</span></span>&#123;</span><br><span class="line">      System.out.println(<span class="string">"Mammal eats"</span>);</span><br><span class="line">   &#125;</span><br><span class="line"> </span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">travel</span><span class="params">()</span></span>&#123;</span><br><span class="line">      System.out.println(<span class="string">"Mammal travels"</span>);</span><br><span class="line">   &#125; </span><br><span class="line"> </span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">noOfLegs</span><span class="params">()</span></span>&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">   &#125;</span><br><span class="line"> </span><br><span class="line">   <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String args[])</span></span>&#123;</span><br><span class="line">      MammalInt m = <span class="keyword">new</span> MammalInt();</span><br><span class="line">      m.eat();</span><br><span class="line">      m.travel();</span><br><span class="line">   &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="内部类"><a href="#内部类" class="headerlink" title="内部类"></a>内部类</h2><p>通过外部类实例化内部类</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Outer outer &#x3D; new Outer();</span><br><span class="line">Outer.Inner inner &#x3D; outer.new Inner();</span><br></pre></td></tr></table></figure>



<h2 id="异常"><a href="#异常" class="headerlink" title="异常"></a>异常</h2><p>Throwable Error Exception</p>
<p>try catch finally throw  throws</p>
<p>如果在函数体内用throw抛出了某种异常，最好要在函数名中加throws抛异常声明，然后交给调用它的上层函数进行处理</p>
<h2 id="关注公众号：-页面仔小杨-【实战干货、原创分享】"><a href="#关注公众号：-页面仔小杨-【实战干货、原创分享】" class="headerlink" title="关注公众号： 页面仔小杨 【实战干货、原创分享】"></a>关注公众号： 页面仔小杨 【实战干货、原创分享】</h2><p><img src="https://raw.githubusercontent.com/itxing666/blogImgs/main/img/bottom.png" alt="bottom"></p>
</div></article></div><div class="right-container"><div class="widget"><div id="arAnchorBar"></div></div></div></section></div><div class="right-menu"></div><div class="modal search-modal"><div class="input-field"><input type="text" id="search_input"><label for="search-input">搜索</label></div><div id="search_result" class="search-result"></div></div><div class="blog-overlay"></div><footer class="row"><div class="footer-con"><div class="paginator"><a href="/2021/01/18/Flutter/Flutter%E5%9F%BA%E7%A1%80/" title="Flutter基础" class="prev">PREV</a><a href="/2021/01/15/js%E5%9F%BA%E7%A1%80/%E5%B0%8F%E6%95%B0%E5%A4%84%E7%90%86%E6%80%BB%E7%BB%93/" title="小数处理总结" class="next">NEXT</a></div><a href="#comment" class="comment-anchor"></a><div id="vcomments"></div><script>new Valine({
    el: "#vcomments",
    appId: "fTqcCRR2oHhKQGgjSy86Ifkf-gzGzoHsz",
    appKey: "r1WK7286QLlfxnpbWsjs06xH",
    notify: false,
    verify: false,
    avatar: "robohash",
    visitor: true,
    placeholder: "随便说点什么～.～",
});</script><div class="copyright"><p>Copyright © 2016 - 2022  <a target="_blank">页面仔小杨</a> 版权所有【实战干货、原创分享】 <br></p><p> <span style="padding-right: 6px;">关注公众号：页面仔小杨</span></p></div><div class="totop"><i></i></div></div></footer><script async src="https://cdn.bootcss.com/mathjax/2.6.1/MathJax.js?config=TeX-MML-AM_CHTML"></script><script src="/scripts/jquery-1.8.2.min.js"></script><script src="/scripts/ar-anchor.js"></script><script src="/scripts/main.js"></script><script>(function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;e=o.createElement(i);r=o.getElementsByTagName(i)[0];e.src='//www.google-analytics.com/analytics.js';r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));ga('create',"UA-65933410-1",'auto');ga('send','pageview');</script></body></html>